﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Assignment_3.Classes;

namespace Assignment_3.Feature___Booking
{
    public partial class CheckIn : System.Web.UI.Page
    {
        double gst = 0.07;
        double servicecharge = 0.1;
        #region Control Methods
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                textPnl.Visible = false;
                editBtn.Visible = false;
            }
        }

        protected void searchBtn_Click(object sender, EventArgs e)
        {
            bookingLbl.Text = "";
            //Get Invoice based on bookingID
            Object[] invoice = DatabaseManager.getInvoiceByBookingID(Convert.ToInt32(searchTbx.Text));
            Booking b = (Booking)invoice[0];
            Guest g = (Guest)invoice[1];
            Room r = (Room)invoice[2];
            Payment p = (Payment)invoice[3];

            //Check if got existing booking ID
            if (b != null)
            {
                infoPnl.Visible = true;
                //Store data into labels
                bookingIDLbl.Text = b.BookingID.ToString();
                guestIDLbl.Text = g.GuestID.ToString();
                guestnameLbl.Text = g.Name;
                phonenoLbl.Text = g.Phone.ToString();
                emailLbl.Text = g.Email;
                addressLbl.Text = g.Address;
                postalcodeLbl.Text = g.PostalCode.ToString();
                countryLbl.Text = g.Country;
                noofadultsLbl.Text = b.No_Of_Adults.ToString();
                noofchildrenLbl.Text = b.No_Of_Children.ToString();
                checkinLbl.Text = b.Check_In_Date.ToString("yyyy-MM-dd");
                checkoutLbl.Text = b.Check_Out_Date.ToString("yyyy-MM-dd");
                noofroomsLbl.Text = b.No_Of_Rooms.ToString();
                roomtypeLbl.Text = r.Room_Type;
                bedtypeLbl.Text = r.Bed_Type;
                paymenttypeLbl.Text = g.PaymentType;
                if (p != null)
                {
                    expirationLbl.Text = p.ExpiryDate.ToString("yyyy-MM-dd");
                    creditcardnoLbl.Text = p.CreditCardNo.ToString();
                }
                else
                {
                    expirationLbl.Text = "-";
                    creditcardnoLbl.Text = "-";
                }
                roomratesLbl.Text = r.Room_Rates.ToString();
                itemsconsumedLbl.Text = "0";
                totalLbl.Text = ((r.Room_Rates * b.No_Of_Rooms)*(1+gst+servicecharge)).ToString();
                editBtn.Visible = true;
            }
            else
            {
                bookingLbl.Text = "No such Booking ID";
                infoPnl.Visible = false;
            }
        }

        //To enter the amount for items that are consumed in the room
        protected void editBtn_Click(object sender, EventArgs e)
        {
            //Show Textbox
            if (labelPnl.Visible == true)
            {
                textPnl.Visible = true;
                labelPnl.Visible = false;
                itemsconsumedTbx.Text = itemsconsumedLbl.Text;
            }
            //Show Label
            else
            {
                textPnl.Visible = false;
                labelPnl.Visible = true;
                if (itemsconsumedTbx.Text.Equals(""))
                {
                    itemsconsumedLbl.Text = "0";
                }
                else
                {
                    itemsconsumedLbl.Text = itemsconsumedTbx.Text;
                }
                //Calculate total price
                totalLbl.Text = ((Convert.ToInt32(itemsconsumedLbl.Text) + Convert.ToInt32(roomratesLbl.Text) * Convert.ToInt32(noofroomsLbl.Text)) * (1 + gst + servicecharge)).ToString();
            }
        }

        //Change Room Status to Vacant & Cleaning
        protected void statusBtn_Click(object sender, EventArgs e)
        {
            ArrayList roomIDList = DatabaseManager.getRoomIDByBookingID(Convert.ToInt32(bookingIDLbl.Text));
            int count = 0;
            foreach (int roomID in roomIDList)
            {
                Room r = new Room();
                r.Status = "Vacant & Cleaning";
                r.RoomID = roomID;
                count += DatabaseManager.UpdateRoomStatus(r);
            }
            if (count > 0)
            {
                bookingLbl.Text = "Room Status has been changed to Vacant & Cleaning.";
            }
            else
            {
                bookingLbl.Text = "No changes have been made.";
            }
        }
        #endregion
    }
}